package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class LoginFrame extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ImageIcon image=new ImageIcon(getClass().getResource("../image/login6.jpg"));
	private JPanel mainJPanel=null;
	private JLabel imgeLabel=new JLabel(image);
	private JPasswordField pf;
	private static JTextField tf;
	private JButton loginBut,cancleBut;
	
	public LoginFrame() {
		// TODO Auto-generated constructor stub
        initialize();
	}

	private void initialize() {
		// TODO Auto-generated method stub
		Toolkit tk=Toolkit.getDefaultToolkit();
		Image i=tk.getImage(this.getClass().getResource("../image/icon.jpg"));
		this.setIconImage(i);
		
		this.setUndecorated(true);
		this.getRootPane().setWindowDecorationStyle(0);
		this.setSize(600, 400);
		this.setResizable(false);
		this.add(getCenterJPanel());
		this.getLayeredPane().add(imgeLabel, new Integer(Integer.MIN_VALUE));
		imgeLabel.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		((JPanel)getContentPane()).setOpaque(false);
		setSize(image.getIconWidth(), image.getIconHeight());
		this.setLocationRelativeTo(null);
	}

	public JPanel getCenterJPanel(){
		if(mainJPanel==null){
			mainJPanel=new JPanel();
			mainJPanel.setLayout(null);
			mainJPanel.setOpaque(false);//设置为透明
			JLabel userlab=new JLabel("用户名");
			userlab.setBounds(160, 150, 80, 50);
            userlab.setFont(new Font("宋体", Font.BOLD, 20));
			mainJPanel.add(userlab);
			tf=new JTextField(20);
			tf.setBounds(240, 160, 200, 30);
			tf.setOpaque(false);
			mainJPanel.add(tf);
			JLabel passwordlab=new JLabel("密  码");
			passwordlab.setFont(new Font("宋体", Font.BOLD, 20));
			passwordlab.setBounds(160, 195, 80, 50);
			mainJPanel.add(passwordlab);
			pf=new JPasswordField(8);
			pf.setBounds(240, 204, 200, 30);
			pf.setEchoChar('*');
			pf.setOpaque(false);
			mainJPanel.add(pf);
			loginBut=new JButton("登  录");
			loginBut.setFont(new Font("宋体", Font.BOLD, 15));
			loginBut.setBounds(380, 350, 80, 40);
			loginBut.setBackground(Color.DARK_GRAY);
			loginBut.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					super.mouseClicked(e);
					String s=new String(pf.getPassword());
					if(tf.getText().trim().equals("manager")&&s.equals("manager")){
						LoginFrame.this.setVisible(false);
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								try {
									UIManager.setLookAndFeel(new NimbusLookAndFeel());
								} catch (UnsupportedLookAndFeelException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								new MainFrame().setVisible(true);
							}
						});
					}
				}
			});
			mainJPanel.add(loginBut);
			cancleBut=new JButton("取  消");
			cancleBut.setFont(new Font("宋体", Font.BOLD, 15));
			cancleBut.setBounds(480, 350, 80, 40);
			cancleBut.setBackground(Color.DARK_GRAY);
			cancleBut.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					super.mouseClicked(e);
					System.exit(0);
				}
			});
			mainJPanel.add(cancleBut);
		}
		return mainJPanel;
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					UIManager.setLookAndFeel(new NimbusLookAndFeel());
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				new LoginFrame().setVisible(true);
			}
		});
	}

	public static String tfText(){
		return tf.getText();
	}
}
